<?php View::display('technic/menu');?>
<?php $cis = $params['configInvoices'];?>
<!-- .head -->
<header class="head">
	<!-- ."main-bar -->
	<div class="main-bar">
		<div class="container-fluid">
			<div class="row-fluid">
				<div class="span12">
					<h3>
						<i class="icon-wrench"></i> แจ้งติดตั้งจากฝ่ายบัญชี
					</h3>
				</div>
			</div>
			<!-- /.row-fluid -->
		</div>
		<!-- /.container-fluid -->
	</div>
	<!-- /.main-bar -->
</header>
<!-- /.head -->
<!-- #content -->
<div id="content">
	<!-- .outer -->
	<div class="container-fluid outer">
	
		<div class="row-fluid">
			<!-- .inner -->
			<div class="span12 inner">
				<!--Begin Datatables-->
				<div class="row-fluid">
					<div class="span12">
						<div class="box">
							<header>
								<h5>แจ้งติดติ้งจากฝ่ายบัญชี</h5>
							</header>
							<div class="body">
								<table class="table table-bordered dataTable responsive">
									<thead>
										<th>invoice id</th>
										<th>pcakage</th>
										<th>info</th>
										<th>status</th>
										<th></th>
									</thead>
									<tbody>
										<?php foreach($cis as $key => $ci){
											$status = $ci['status']==1? 'ติดตั้งแล้ว': 'รอการติดตั้ง';
											$package = $ci['invoice']['package']['type'].' - '.$ci['invoice']['package']['name'];
											$invoice = sprintf('<a targer="_blank" href="old.php?page=invoice&id=%s">%s</a>', $ci['invoice']['id'], $ci['invoice']['id']);
										?>
										<tr class="list-row" rowId="<?php echo $ci['id'];?>">
											<td><?php echo $invoice;?></td>
											<td><?php echo $package;?></td>
											<td><?php echo nl2br($ci['invoice']['info']);?></td>
											<td><?php echo $status;?></td>
											<td><button class="btn btn-info configSuccess">ติดตั้งแล้ว</button></td>
										</tr>
										<?php }?>
									</tbody>
								</table>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
		<!--Begin Datatables-->
	</div>
</div>
<script type="text/template" class="modalAddProduct">
    <div class="modal hide fade">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <h3>ติดตั้งบริการ</h3>
        </div>
        <div class="modal-body">
            <form>
                <label>package</label>
                <div class="alert alert-info package"></div>
                <label>account</label>
                <div class="alert alert-info email"></div>
                <label>month</label>
                <div class="alert alert-info month"></div>
                <label>note</label>
                <textarea class="note"></textarea>
            </form>
        </div>
        <div class="modal-footer">
            <button class="btn cancel">Cancel</button>
            <button class="btn btn-primary paidNew">Submit</button>
        </div>
    </div>
</script>
<script type="text/javascript">
$(function(){
	var oTable = $('.dataTable').dataTable({"bFilter": false, "bInfo": false, "bSort": false,
		sDom: "<'pull-right'l>t<'row-fluid'<'span6'f><'span6'p>>",
        sPaginationType: "bootstrap",
        oLanguage: {
            "sLengthMenu": "Show _MENU_ entries"
        }
    });

	function delRow(row){
		$(row).fadeOut(function(){
			row = $(row).get(0);
			oTable.fnDeleteRow(oTable.fnGetPosition(row));
		});
	}

    function initIP(modal, ips, defaultIp)
    {
        function format(item) { return item.ip; };
        var select2 = $('.ipId', modal).select2({
            placeholder: 'Search from ip',
            data: { results: ips, text: function(item){ return item.ip; } },
            formatSelection: format,
            formatResult: format
        });
        if(typeof defaultIp != 'undefined')
            select2.select2('val', defaultIp);
    }

	$('.list-row').delegate('.configSuccess', 'click', function(event){
		var dTarget = $(event.delegateTarget);
		var target = $(event.target);
		var id = dTarget.attr('rowId');

        target.button('loading');
        $.get('old.php?page=technic/configInvoice.rest', {id: id}, function(res){
            var configIV = res.data.configInvoice;
            var ips = res.data.ips;
            var invoice = configIV.invoice;
            var account = invoice.account;

            target.button('reset');
            var modal = $($('.modalAddProduct').html());
            var sendData = {};
            sendData.id = id;
            sendData.invoiceId = invoice.id;

            modal.modal();
            modal.on('hidden', function(data){
                modal.remove();
            });

            $('.cancel', modal).click(function(event){
                $('.close', modal).click();
            });
;
            $('.package', modal).html(
                '<strong>'+invoice.package.type+'</strong><br />'+
                    '<small>'+invoice.package.name+'</small>'
            );
            sendData.packageId = invoice.packageId;

            $('.email', modal).html(
                '<a target="_blank" href="old.php?page=accounting/account/detail&id='+account.id+'">'+account.email+'</a>'
            );
            sendData.accountId = configIV.invoice.account.id;

            $('.month', modal).text(invoice.month+' เดือน');
            sendData.month = invoice.month;

            var ipIdElOri = $('<input type="hidden" class="ipId" />');

            if(invoice.type=='colo'){
                sendData.type='colo';
                $('.note', modal).after(ipIdElOri);
                initIP(modal, ips);
            }
            else if(invoice.type=='vps'){
                var os;
                if(invoice.os == 2){ os = 'Linux'; }
                else if(invoice.os == 3){ os = 'Window'; }

                $('.note', modal).prev('label').before('<label>os</label><div class="alert alert-info">'+os+'</div>');
                $('.note', modal).prev('label').before('<label>ip</label>');
                $('.note', modal).prev('label').before(ipIdElOri);
                initIP(modal, ips);

                sendData.os = invoice.os;
                sendData.type='vps';
            }
            else if(invoice.type=='hosting'){
                $('.note', modal).prev('label').before('<label>Domain</label><div class="alert alert-info">'+invoice.domain+'</div>');
                sendData.domain = invoice.domain;
                sendData.type='hosting';
            }

            var sending = false;
            $('.paidNew', modal).click(function(event){
                if(sending) return;
                var button = $(this);

                sendData.note = $('.note', modal).val();
                if(invoice.type=='hosting')
                    sendData.domain = invoice.domain;
                else
                    sendData.ipId = ipIdElOri.val();

                button.button('loading');
                $.post('old.php?page=technic/configInvoice.rest', sendData, function(data){
                    button.button('reset');
                    if(typeof data.error=='undefined'){
                        delRow(dTarget);
                        modal.modal('hide');
                        delRow(dTarget);
                        return;
                    }
                    else {
                        alert(data.error.message);
                    }
                }, 'JSON');
            });
        }, 'JSON');
	});
});
</script>